package com.syyo.gulimall.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.syyo.common.enums.ResultEnum;
import com.syyo.common.exception.SysException;
import com.syyo.gulimall.product.domain.entity.BrandEntity;
import com.syyo.gulimall.product.domain.entity.CategoryBrandRelationEntity;
import com.syyo.gulimall.product.domain.entity.CategoryEntity;
import com.syyo.gulimall.product.domain.req.CategoryBrandRelationReq;
import com.syyo.gulimall.product.domain.resp.BrandResp;
import com.syyo.gulimall.product.mapper.BrandMapper;
import com.syyo.gulimall.product.mapper.CategoryBrandRelationMapper;
import com.syyo.gulimall.product.mapper.CategoryMapper;
import com.syyo.gulimall.product.service.CategoryBrandRelationService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;


/**
 * 品牌分类关联
 *
 * @author syyo
 * @email syyo@gmail.com
 * @date 2021-01-20 09:32:12
 */
@Service
public class CategoryBrandRelationServiceImpl extends ServiceImpl<CategoryBrandRelationMapper, CategoryBrandRelationEntity> implements CategoryBrandRelationService {

    @Autowired
    private CategoryBrandRelationMapper categoryBrandRelationMapper;
    @Autowired
    private BrandMapper brandMapper;
    @Autowired
    private CategoryMapper categoryMapper;


    /**
     * 新增
     */
    @Override
    @Transactional
    public int add(CategoryBrandRelationReq req) {
        CategoryBrandRelationEntity categoryBrandRelation = new CategoryBrandRelationEntity();

        categoryBrandRelation.setBrandId(req.getBrandId());
        categoryBrandRelation.setCatelogId(req.getCatelogId());
        // 查询分类名称
        BrandEntity brandEntity = brandMapper.selectById(req.getBrandId());
        // 查询品牌名称
        CategoryEntity categoryEntity = categoryMapper.selectById(req.getCatelogId());

        categoryBrandRelation.setBrandName(brandEntity.getName());
        categoryBrandRelation.setCatelogName(categoryEntity.getName());
        // 添加创建时间和更新时间
        int insert = categoryBrandRelationMapper.insert(categoryBrandRelation);
        if (insert != 1) {
            throw new SysException(ResultEnum.E_10001.getCode(), ResultEnum.E_10001.getMessage());
        }
        return insert;
    }

    /**
     * 删除
     */
    @Override
    @Transactional
    public int del(Integer id) {
        int insert = categoryBrandRelationMapper.deleteById(id);
        if (insert != 1) {
            throw new SysException(ResultEnum.E_10002.getCode(), ResultEnum.E_10002.getMessage());
        }
        return insert;
    }

    /**
     * 编辑
     */
    @Override
    @Transactional
    public int edit(CategoryBrandRelationReq req) {
        CategoryBrandRelationEntity categoryBrandRelation = new CategoryBrandRelationEntity();
        BeanUtils.copyProperties(req, categoryBrandRelation);

        int insert = categoryBrandRelationMapper.updateById(categoryBrandRelation);
        if (insert != 1) {
            throw new SysException(ResultEnum.E_10003.getCode(), ResultEnum.E_10003.getMessage());
        }
        return insert;
    }

    /**
     * 详情
     */
    @Override
    public CategoryBrandRelationEntity findOne(Integer id) {
        return categoryBrandRelationMapper.selectById(id);
    }

    /**
     * 列表
     */
    @Override
    public List<CategoryBrandRelationEntity> findPage(CategoryBrandRelationReq req) {
        QueryWrapper<CategoryBrandRelationEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("brand_id", req.getBrandId());
        return categoryBrandRelationMapper.selectList(wrapper);
    }

    @Override
    public List<BrandResp> findAll(Long catId) {
        List<CategoryBrandRelationEntity> list = categoryBrandRelationMapper.selectList(new QueryWrapper<CategoryBrandRelationEntity>().eq("catelog_id", catId));
        List<BrandResp> collect = list.stream().map((item) -> {
            BrandResp resp = new BrandResp();
            BeanUtils.copyProperties(item, resp);
            return resp;
        }).collect(Collectors.toList());

        return collect;
    }

}